import numpy as np
import cv2

# 原始图像加入彩色噪声
def gauss_noise(image, mean=0, var=0.005):
    """
    添加高斯噪声
    mean : 均值
    var : 方差
    """
    image = np.array(image/255, dtype=float)
    noise = np.random.normal(mean, var ** 0.5, image.shape)
    out = image + noise
    if out.min() < 0:
        low_clip = -1.
    else:
        low_clip = 0.
    out = np.clip(out, low_clip, 1.0)
    out = np.uint8(out*255)
    # cv2.imshow("Gauss", out)
    return out


filename = r'E:\python\data\lena.jpg'
img = cv2.imread(filename)
img = gauss_noise(img)  # 原图像加入高斯噪声

blur = cv2.blur(img, (5,5))  # 平均滤波
gauss = cv2.GaussianBlur(img, (5,5), 0)  # 高斯滤波
median = cv2.medianBlur(img, 5)  # 中值滤波
bilateral = cv2.bilateralFilter(img, 5, 150, 150)  # 双边滤波

cv2.imshow('Image', img)
cv2.imshow('Blurred', blur)
cv2.imshow('Gauss', gauss)
cv2.imshow('Median filtered', median)
cv2.imshow('Bilateral filtered', bilateral)

cv2.waitKey()
cv2.destroyAllWindows()


